//
//  HealthKnowledgeClassViewController.m
//  LoveHealth
//
//  Created by 陈永坤 on 14-9-18.
//  Copyright (c) 2014年 lanou. All rights reserved.
//

#import "HealthKnowledgeClassViewController.h"
#import "HealthKnowledgeClass.h"
#import "HeakthKnowledgeClassViewCell.h"


#import "HealthKnowledgeListViewController.h"
@interface HealthKnowledgeClassViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic,retain)NSMutableArray * classArray;
@property (nonatomic,retain)UICollectionView * collecionView;
@end

@implementation HealthKnowledgeClassViewController

- (void)dealloc
{
    self.classArray = nil;
    self.collecionView = nil;
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"常识分类";
    self.classArray = [NSMutableArray arrayWithCapacity:10];
    
    NSString * filePath = [[NSBundle mainBundle]pathForResource:@"HealthKnowledgeClass.txt" ofType:nil];
    NSData * data = [NSData dataWithContentsOfFile:filePath];
    
    NSArray *  sourceArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    for (NSDictionary * dic in sourceArray) {
                HealthKnowledgeClass * knowledgeClass = [[HealthKnowledgeClass alloc]init];
                [knowledgeClass setValuesForKeysWithDictionary:dic];
                [self.classArray addObject:knowledgeClass];
                NSLog(@"%@,%@",knowledgeClass.ID, knowledgeClass.name);
               [knowledgeClass release];
            }
    


    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.itemSize = CGSizeMake(100, 100);
    UICollectionView * collecionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    collecionView.backgroundColor = [UIColor whiteColor];
    collecionView.dataSource = self;
    collecionView.delegate = self;
    [self.view addSubview:collecionView];
    [flowLayout release];
    [collecionView release];
    
    //注册集合视图cell
    [collecionView registerClass:[HeakthKnowledgeClassViewCell class] forCellWithReuseIdentifier:@"cell"];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.classArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HeakthKnowledgeClassViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    //cell.backgroundColor = [UIColor brownColor];
    
    HealthKnowledgeClass * knowledgeClass = self.classArray[indexPath.row];
    cell.showImageView.image = [UIImage imageNamed:knowledgeClass.name];
    cell.showLabel.text = knowledgeClass.name;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HealthKnowledgeClass * knowledgeClass = self.classArray[indexPath.row];
    NSLog(@"%@",knowledgeClass.ID);
    HealthKnowledgeListViewController * listVC = [[HealthKnowledgeListViewController alloc]init];
    listVC.knowledgeClass = knowledgeClass;
    [self.navigationController pushViewController:listVC animated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
